package org.dylan.bdf.system.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.dylan.common.entity.AbstractUUIDEntity;
import org.dylan.security.entity.IOperation;
import org.dylan.security.entity.IPermission;
import org.dylan.security.entity.IResource;
import org.dylan.security.entity.IRole;

@Entity
@Table(name = "SYS_ROLE")
public class Role extends AbstractUUIDEntity implements IRole {
	private static final long serialVersionUID = -2652613104976969075L;
	private String name;
	private String remark;
	@ManyToMany(targetEntity = Resource.class)
	@JoinTable(name = "SYS_ROLE_RESOURCE", joinColumns = { @JoinColumn(name = "ROLE_ID") }, inverseJoinColumns = @JoinColumn(name = "RESOURCE_ID"))
	private List<IResource> resources = new ArrayList<IResource>();

	@ManyToMany(targetEntity = Resource.class)
	@JoinTable(name = "SYS_ROLE_OPERATION", joinColumns = { @JoinColumn(name = "ROLE_ID") }, inverseJoinColumns = @JoinColumn(name = "OPERATION_ID"))
	private List<IOperation> operations = new ArrayList<IOperation>();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public List<IPermission> getPermissions() {
		ArrayList<IPermission> permissions = new ArrayList<IPermission>();
		if (this.resources != null) {
			permissions.addAll(resources);
		}
		if (this.operations != null) {
			permissions.addAll(operations);
		}
		return permissions;
	}

	public List<IResource> getResources() {
		return resources;
	}

	public void setResources(List<IResource> resources) {
		this.resources = resources;
	}

	public List<IOperation> getOperations() {
		return operations;
	}

	public void setOperations(List<IOperation> operations) {
		this.operations = operations;
	}

	public void addPermissions(List<IPermission> permissions) {
		if (permissions != null) {
			for (IPermission permission : permissions) {
				if (permission instanceof IResource) {
					addResource((IResource) permission);
				} else if (permission instanceof IOperation) {
					addOperation((IOperation) permission);
				}
			}
		}
	}

	public void addResources(List<IResource> resources) {
		if (this.resources == null) {
			this.resources = new ArrayList<IResource>();
		}
		this.resources.addAll(resources);
	}

	public void addResource(IResource resource) {
		if (this.resources == null) {
			this.resources = new ArrayList<IResource>();
		}
		resources.add(resource);
	}

	public void addOperations(List<IOperation> operations) {
		if (this.operations == null) {
			this.operations = new ArrayList<IOperation>();
		}
		this.operations.addAll(operations);
	}

	public void addOperation(IOperation operation) {
		if (this.operations == null) {
			this.operations = new ArrayList<IOperation>();
		}
		operations.add(operation);
	}

}
